﻿using KayakMes.Common;
using KayakMes.Common.Dtos.BasicSettingDTO.Input;
using KayakMes.Common.Result;
using KayakMes.Units.Domain.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace KayakMes.Units.Application
{
    /// <summary>
    /// 基础设置服务实现类
    /// 实现计量单位相关的业务逻辑操作
    /// </summary>
    public class BasicSettingService : IBasicSettingService
    {
        private readonly BaseReposiotry<UnitsModel> unitsRepo;
        /// <summary>
        /// 通过构造函数注入计量单位仓储
        /// </summary>
        /// <param name="unitsRepo">计量单位仓储</param>
        public BasicSettingService(BaseReposiotry<UnitsModel> unitsRepo)
        {
            this.unitsRepo = unitsRepo;
        }
        #region 计量单位相关操作
        /// <summary>
        /// 添加计量单位
        /// </summary>
        /// <param name="units">计量单位模型</param>
        /// <returns>返回操作结果，成功返回1，失败返回0</returns>
        public ApiResult<int> AddUnits(UnitsModel units)
        {
            Random rnd = new Random();
            units.Id-=YitIdHelper.NextId();
            units.UnitsCode=rnd.Next(1000,9999).ToString();
            units.CreateTime = DateTime.Now;
            units.UpdateTime = DateTime.Now;
            var result =unitsRepo.Add(units);

            if (result>0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "添加成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "添加失败", Data = 0 };
            }
        }
        /// <summary>
        /// 获取所有计量单位（支持分页查询）
        /// </summary>
        /// <param name="dto">计量单位查询条件DTO</param>
        /// <returns>返回分页查询的计量单位列表</returns>
        public ApiPaging<UnitsModel> GetAllUnits(UnitsDto dto)
        {
            var result = unitsRepo.GetAll();
            if(!string.IsNullOrEmpty(dto.UnitsCode))
            {
                result = result.Where(x => x.UnitsCode.Contains(dto.UnitsCode));
            }
            if (!string.IsNullOrEmpty(dto.UnitsName))
            {
                result = result.Where(x => x.UnitsName.Contains(dto.UnitsName));
            }
            ApiPaging<UnitsModel> apiPaging = new ApiPaging<UnitsModel>
            {
                Total = result.Count(),
                PageIndex = dto.Index,
                PageSize = dto.Size,
                Items = result.Skip((dto.Index - 1) * dto.Size).Take(dto.Size).ToList()
            };
            return apiPaging;
        }
        /// <summary>
        /// 根据ID获取指定的计量单位
        /// </summary>
        /// <param name="Id">计量单位ID</param>
        /// <returns>返回操作结果，包含指定的计量单位订单信息</returns>
        public ApiResult<UnitsModel> GetUnits(long Id)
        {
            var result=unitsRepo.GetModel(Id);
            if(result==null)
            {
                return new ApiResult<UnitsModel> { Code = ApiEnums.NotFound, Message = "计量单位未找到", Data = null };
            }
            if(result.IsDeleted==true)
            {
                return new ApiResult<UnitsModel> { Code = ApiEnums.NotFound, Message = "计量单位已被删除", Data = null };
            }
            return new ApiResult<UnitsModel> { Code = ApiEnums.Success, Message = "计量单位查询成功", Data = result };  
        }
        /// <summary>
        /// 更新计量单位
        /// </summary>
        /// <param name="units">计量单位模型</param>
        /// <returns>返回</returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> UpdateUnits(UnitsModel units)
        {
            var result=unitsRepo.Update(units);
            if(result > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "更新成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "更新失败", Data = 0 };
            }
        }
        /// <summary>
        /// 逻辑删除计量单位
        /// </summary>
        /// <param name="Id">计量单位ID</param>
        /// <returns>返回操作结果，成功返回1，失败返回0</returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> LoginDel(long Id)
        {
            var result = unitsRepo.GetModel(Id);
            if (result == null || result.IsDeleted == true)
            {
                return new ApiResult<int> { Code = ApiEnums.NotFound, Message = "计量单位未找到或已被删除", Data = 0 };
            }
            result.IsDeleted = true;
            var updateResult = unitsRepo.Update(result);
            if (updateResult > 0)
            {
                return new ApiResult<int> { Code = ApiEnums.Success, Message = "计量单位删除成功", Data = 1 };
            }
            else
            {
                return new ApiResult<int> { Code = ApiEnums.Fail, Message = "计量单位删除失败", Data = 0 };
            }
        }
        #endregion
    }
}
